package com.ithiema.reggie.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithiema.reggie.Mapper.SetmealMapper;
import com.ithiema.reggie.Service.SetmealDishService;
import com.ithiema.reggie.Service.SetmealService;
import com.ithiema.reggie.common.CustomException;
import com.ithiema.reggie.common.R;
import com.ithiema.reggie.dto.SetmealDto;
import com.ithiema.reggie.entity.DishFlavor;
import com.ithiema.reggie.entity.Setmeal;
import com.ithiema.reggie.entity.SetmealDish;
import com.sun.prism.impl.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
   @Autowired
   private SetmealDishService setmealDishService;
   @Autowired
   private SetmealService setmealService;

    //套餐管理-保存套餐和菜品关群关系
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐
        save(setmealDto);
        //保存套餐和菜品的关联关系
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
            //forEach{遍历数组}
        setmealDishes.forEach(item -> item.setSetmealId(setmealDto.getId()));
        //saveBatch {保存批次}
        setmealDishService.saveBatch(setmealDishes);
    }

    //(删除单个套餐)批量删除套餐
    @Override
    public void removeWithDish(List<Long> ids) {
        //两个类中都要删除

        if (ids.isEmpty()){
            throw new CustomException("批量删除未勾选，不能删除");
        }
        LambdaQueryWrapper<Setmeal> lwp1 = new LambdaQueryWrapper<>();
        lwp1.in(Setmeal::getId,ids);
        //菜品状态需为 1
        lwp1.eq(Setmeal::getStatus,1);
        if (count(lwp1)>0){
            //如果不能删除，抛异常
            throw new CustomException("套餐售卖中，不能删除");
        }
        removeByIds(ids);

        LambdaQueryWrapper<SetmealDish> lwp2 = new LambdaQueryWrapper<>();
        lwp2.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lwp2);
    }

    //启用停用&&批量启用停用
    @Override
    public void salesWithDish(Integer status, List<Long> ids) {
        LambdaUpdateWrapper<Setmeal> lwq = new LambdaUpdateWrapper<>();
        lwq.set(Setmeal::getStatus,status).in(Setmeal::getId,ids);
        setmealService.update(lwq);

        // 看得懂的方法
//        lwq.in(ids!=null, Setmeal::getId,ids);
//        List<Setmeal> list = setmealService.list(lwq);
//        for (Setmeal setmeal : list) {
//            //如果是停用状态
//            if (setmeal!=null){
//                setmeal.setStatus(status);
//                setmealService.updateById(setmeal);
//            }
//        }


    }

    //套餐修改之  {数据回显}
    @Override
    public SetmealDto getData(Long id) {
        Setmeal setmeal = getById(id);

        LambdaQueryWrapper<SetmealDish> lwp = new LambdaQueryWrapper<>();
        //在关联表中查询，setmealdish
        lwp.eq(id!=null,SetmealDish::getDishId,id);

        SetmealDto setmealDto = new SetmealDto();
        if (setmeal!=null){
            BeanUtils.copyProperties(setmeal,setmealDto);
            List<SetmealDish> list = setmealDishService.list(lwp);
            setmealDto.setSetmealDishes(list);
            return setmealDto;
        }


        return null;
    }


    //修改套餐
    @Override
    public void edit(SetmealDto setmealDto) {

        updateById(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        Long setmealId  = setmealDto.getId();


        LambdaQueryWrapper<SetmealDish> lwp = new LambdaQueryWrapper<>();
        lwp.eq(SetmealDish::getSetmealId,setmealId);
        setmealDishService.remove(lwp);

        //为setmeal_dish表填充相关的属性
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        //批量把setmealDish保存到setmeal_dish表
        setmealDishService.saveBatch(setmealDishes);
        setmealService.updateById(setmealDto);

    }


}